#include "GUIElement.h"
#include <DXUT.h>

e3d::GUIElement::GUIElement(  ID3D10Device* device, ID3D10Effect* effect )
:	m_ndcBuffer( NULL ), 
	m_ndcLayout( NULL ),
	m_effect( effect ),
	m_device( device )
{
}

e3d::GUIElement::~GUIElement(void)
{
}

void e3d::GUIElement::render( ID3D10EffectTechnique* tech, const D3DXVECTOR3* camPos )
{

	// Bind texture as pixel shader input resource
	//m_textureVariable->SetResourceArray( &m_textureSRV, 1, 1 );

	ID3D10EffectTechnique* cmTech = m_effect->GetTechniqueByName("GUI");
	D3D10_TECHNIQUE_DESC desc;
	cmTech->GetDesc(&desc);
	cmTech->GetPassByName("standard")->Apply(0);

	unsigned int stride = sizeof( e3d::Vertex  );
	unsigned int offset = 0;

	m_device->IASetInputLayout( m_ndcLayout );
	m_device->IASetVertexBuffers( 0, 1, &m_ndcBuffer, &stride, &offset );
	m_device->IASetPrimitiveTopology( D3D10_PRIMITIVE_TOPOLOGY_TRIANGLELIST );

	m_device->Draw( 6, 0 );
}

bool e3d::GUIElement::createRessources( ID3D10EffectTechnique* tech )
{
	bool success = true;

	//------------------------------------------------
	// Create vertex buffer for env mapping
	//------------------------------------------------
	e3d::Vertex ndcBuffer[6];
	ndcBuffer[0].position = D3DXVECTOR3( 0.5f, 1.0f, 0.1f);
	ndcBuffer[0].textureCoordinates = D3DXVECTOR2( 0, 0 );

	ndcBuffer[1].position = D3DXVECTOR3( 1.0f, 1.0f, 0.1f);
	ndcBuffer[1].textureCoordinates = D3DXVECTOR2( 1, 0 );

	ndcBuffer[2].position = D3DXVECTOR3( 0.5f, 0.5f, 0.1f);
	ndcBuffer[2].textureCoordinates = D3DXVECTOR2( 0, 1 );

	ndcBuffer[3].position = D3DXVECTOR3( 0.5f, 0.5f, 0.1f);
	ndcBuffer[3].textureCoordinates = D3DXVECTOR2( 0, 1 );

	ndcBuffer[4].position = D3DXVECTOR3( 1.0f, 1.0f, 0.1f);
	ndcBuffer[4].textureCoordinates = D3DXVECTOR2( 1, 0 );

	ndcBuffer[5].position = D3DXVECTOR3( 1.0f, 0.5f, 0.1f);
	ndcBuffer[5].textureCoordinates = D3DXVECTOR2( 1, 1 );

	D3D10_INPUT_ELEMENT_DESC ndcInputDesc[] = 
	{
		{ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D10_INPUT_PER_VERTEX_DATA, 0 },
		{ "NORMAL", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12, D3D10_INPUT_PER_VERTEX_DATA, 0 },
		{ "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 24, D3D10_INPUT_PER_VERTEX_DATA, 0 },
		{ "COLOR", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 36, D3D10_INPUT_PER_VERTEX_DATA, 0 }
		
	};
	unsigned int numElements = sizeof(ndcInputDesc) / sizeof(ndcInputDesc[0]);

	D3D10_BUFFER_DESC ndcDesc;
	ndcDesc.Usage = D3D10_USAGE_DEFAULT;
	ndcDesc.ByteWidth = sizeof( e3d::Vertex ) * 6;
	ndcDesc.BindFlags = D3D10_BIND_VERTEX_BUFFER;
	ndcDesc.CPUAccessFlags = 0;
	ndcDesc.MiscFlags = 0;
	D3D10_SUBRESOURCE_DATA ndcInitData;
	ndcInitData.pSysMem = ndcBuffer;
	if( FAILED( m_device->CreateBuffer( &ndcDesc, &ndcInitData, &m_ndcBuffer ) ) )
		success = false;

	ID3D10EffectTechnique* cmTech = m_effect->GetTechniqueByName("GUI");
	ID3D10EffectPass* cmPass = cmTech->GetPassByName( "standard" );

	if( !cmTech->IsValid() || !cmPass->IsValid() )
		success = false;
	else
	{
		D3D10_PASS_DESC cmPassDesc;
		cmPass->GetDesc( &cmPassDesc );
		if( FAILED( m_device->CreateInputLayout( ndcInputDesc, numElements, cmPassDesc.pIAInputSignature, cmPassDesc.IAInputSignatureSize, &m_ndcLayout)))
			success = false;
	}

	if( !success )
		releaseRessources( );

	return success;
}

void e3d::GUIElement::releaseRessources()
{
	// Unbind texture resource view
	ID3D10ShaderResourceView * const nullRes[1] = {NULL};
	m_device->PSSetShaderResources( 0, 1, nullRes );

	SAFE_RELEASE( m_ndcLayout );
	SAFE_RELEASE( m_ndcBuffer );
}



